package demo.hdz.mynotification;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


/**
 * @author hdz
 * @date 2019/02/27
 *
 * https://www.jianshu.com/p/6aec3656e274
 * https://blog.csdn.net/u011043551/article/details/71244868
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private NotificationManager mNotificationManager;

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constant.ACTION)) {
                String str = intent.getStringExtra(Constant.DATA);
                //对收到的广播消息进行处理
                Logger.e("BroadcastReceiver: " + str);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSimpleNtf = (Button) findViewById(R.id.btnSimpleNtf);
        btnSimpleNtf.setOnClickListener(this);
        Button btnSimpleNtf2 = (Button) findViewById(R.id.btnSimpleNtf2);
        btnSimpleNtf2.setOnClickListener(this);
        Button btnActionNtf = (Button) findViewById(R.id.btnActionNtf);
        btnActionNtf.setOnClickListener(this);
        Button btnCustomHeadsUpViewNtf = (Button) findViewById(R.id.btnCustomHeadsUpViewNtf);
        btnCustomHeadsUpViewNtf.setOnClickListener(this);
        Button btnProgressViewNtf = (Button) findViewById(R.id.btnProgressViewNtf);
        btnProgressViewNtf.setOnClickListener(this);
        Button btnBigPictureStyleNtf = (Button) findViewById(R.id.btnBigPictureStyleNtf);
        btnBigPictureStyleNtf.setOnClickListener(this);
        Button btnQuickReplyNtf = (Button) findViewById(R.id.btnQuickReplyNtf);
        btnQuickReplyNtf.setOnClickListener(this);
        Button btnCustomViewNtf = (Button) findViewById(R.id.btnCustomViewNtf);
        btnCustomViewNtf.setOnClickListener(this);

        Button btnBigTextStyleNtf = (Button) findViewById(R.id.btnBigTextStyleNtf);
        btnBigTextStyleNtf.setOnClickListener(this);
        Button btnInboxStyleNtf = (Button) findViewById(R.id.btnInboxStyleNtf);
        btnInboxStyleNtf.setOnClickListener(this);
        Button btnMediaStyleNtf = (Button) findViewById(R.id.btnMediaStyleNtf);
        btnMediaStyleNtf.setOnClickListener(this);
        Button btnMessagingStyleNtf = (Button) findViewById(R.id.btnMessagingStyleNtf);
        btnMessagingStyleNtf.setOnClickListener(this);

        Button btnClearAll = (Button) findViewById(R.id.btnClearAll);
        btnClearAll.setOnClickListener(this);

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannels.createAllNotificationChannels(this);
        }
        registerReceiver(broadcastReceiver, new IntentFilter(Constant.ACTION));
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(broadcastReceiver);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSimpleNtf:
                Notifications.sendSimpleNotification(this, mNotificationManager, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound));
                break;
            case R.id.btnSimpleNtf2:
                Notifications.sendSimpleNotification2(this, mNotificationManager);
                break;
            case R.id.btnActionNtf:
                Notifications.sendActionNotification(this, mNotificationManager);
                break;
            case R.id.btnCustomHeadsUpViewNtf:
                Notifications.sendCustomHeadsUpViewNotification(this, mNotificationManager);
                break;
            case R.id.btnProgressViewNtf:
                Intent intent = new Intent(this, NotificationService.class);
                intent.setAction(Constant.ACTION_SEND_PROGRESS_NOTIFICATION);
                startService(intent);
                break;
            case R.id.btnBigPictureStyleNtf:
                Notifications.sendBigPictureStyleNotification(this,mNotificationManager);
                break;
            case R.id.btnQuickReplyNtf:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                    Notifications.sendRemoteInputNotification(this, mNotificationManager);
                }
                break;
            case R.id.btnCustomViewNtf:
                Notifications.sendCustomViewNotification(
                        this,
                        mNotificationManager,
                        new Notifications.NotificationContentWrapper(
                                BitmapFactory.decodeResource(getResources(),
                                        R.drawable.custom_view_picture_current),
                                "最美的期待",
                                "周笔畅 - 最美的期待"),
                        false,
                        true);
                break;
            case R.id.btnBigTextStyleNtf:
                Notifications.sendBigTextStyleNotification(this, mNotificationManager);
                break;
            case R.id.btnInboxStyleNtf:
                Notifications.sendInboxStyleNotification(this, mNotificationManager);
                break;
            case R.id.btnMediaStyleNtf:
                Notifications.sendMediaStyleNotification(this, mNotificationManager,true);
                break;
            case R.id.btnMessagingStyleNtf:
                Notifications.sendMessagingStyleNotification(this, mNotificationManager);
                break;
            case R.id.btnClearAll:
                mNotificationManager.cancelAll();
                break;
            default:
                break;
        }
    }
}
